#coding=utf-8
import pickle,os
from stscraper import get_sohutags
from stutils import set_django_environ
set_django_environ()
from stdj.stock.models import *

from stsettings import datadir
fn  = datadir + '/sohutag.pk'
if not os.path.exists(fn):
    print "No shoutags data local.Get it from sohu Now."
    tcs = get_sohutags()
    pickle.dump(tcs, open(fn))
else:
    tcs = pickle.load(open(fn))

for k in tcs.keys():
    print k
    t = Tag.objects.get_or_create(name=k)[0]
    for code in tcs[k]:
        try:
            s = Share.objects.get(code=code)
        except Share.DoesNotExist:
            continue
        else:
            t.share_set.add(s)

    
    